<?php
// dev.php --- Time-stamp: <2009-01-11 21:53:44 root>
// Copyright 2008 Julian Qian
// Author: lancer@pdn
// Version: $Id: status.php,v 0.0 2008/12/26 06:09:08 lancer Exp $
// Keywords: 

class Dev extends Model {
    function Dev(){
        parent::Model();
    }
    
    function get_status($devid){
        $sql = "select * from status where devid=$devid order by up_time desc limit 20";
        $query = $this->db->query($sql);
        $ret = array();

        include_once("/var/tmp/common_header.php");

        $index = 0;
        foreach($query->result() as $row){
            $stat = strtolower($stat_def[$row->stat]);
            $ret[$index]["status"] = $this->lang->line($stat);
            $ret[$index]["up_time"] = unix_to_human($row->up_time);
            $index++;
        }
        return $ret;
    }

    function curr_stat($devid){
        $sql= "select stat from status where devid=$devid order by up_time desc limit 1";
        $query = $this->db->query($sql);

        include_once("/var/tmp/common_header.php");

        $row = $query->row();

        if($row){
            $str = $stat_def[$row->stat];
            return $this->lang->line(strtolower($str));
        }else{
            return "No data!";
        }
    }

    function get_userinfo($uid){
        $sql = "select * from phone where uid=$uid";
        $query = $this->db->query($sql);
        
        return $query->result_array();
    }

    function get_devinfo($devid){
        $sql = "select * from phone where devid=$devid";
        $query = $this->db->query($sql);
        
        return $query->result_array();
    }

    function new_mobile($uid, $devid, $mobile){
        $sql = "insert into phone (devid, uid, mobile) values ($devid, $uid, '$mobile')";
        $this->db->query($sql);
    }

    function delete_mobile($devid, $mobile){
        $sql = "delete from phone where devid=$devid and mobile='$mobile'";
        $this->db->query($sql);
    }

    function warn_titles(){
        $fields = $this->db->list_fields("phone");
        $titles = array();
        foreach($fields as $field){
            if(substr($field,0,1) == "w"){
                array_push($titles, $field);
            }
        }
        return $titles;
    }

    function set_warn($devid, $mobile, $warn, $warnstr){
        $sql = "update phone set $warn='$warnstr' where devid=$devid and mobile='$mobile'";
        $this->db->query($sql);
    }

    function add_dev($devid, $username, $mobile){
        $sql = "select uid from user where username='$username'";
        $query = $this->db->query($sql);
        if($row = $query->row()){
            $uid = $row->uid;
            // check devid
            $sql = "select uid from phone where devid=$devid";
            $query = $this->db->query($sql);
            if($row = $query->row()){
                if($row->uid != $uid){
                    return "add_dev_binded";
                }
            }
            // check devid + uid
            $sql = "select uid from phone where uid=$uid and devid=$devid and mobile='$mobile'";
            $query = $this->db->query($sql);
            if($query->num_rows()){
                return "add_dev_binded";
            }
            // check devid + uid + mobile
            $sql = "insert into phone (uid, devid, mobile) values ($uid, $devid, '$mobile')";
            $this->db->query($sql);
            return "add_dev_ok";
        }else{
            return "add_dev_no_username";
        }
    }
  }

?>